﻿Shader "Dan/UI/UI_RadialLoading"
{
    Properties
    {
         _Color ("Color", Color) = (0,0.4,1,1)
        _Color2 ("Color2", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _Level ("Level", Float ) = 8
        _Width ("Width", Range(0, 1)) = 0.5
        _Radius ("Radius", Range(0, 1)) = 1
        _CenterRadius ("CenterRadius", Range(0, 1)) = 0.2066802
        [MaterialToggle] _UseSin ("UseSin", Float ) = 0
        _FadeIn ("FadeIn", Range(0, 1)) = 0
        _Speed ("Speed", Float ) = 2
        
        _Patterns ("Patterns", 2D) = "white" {}
        _ScaleIcon ("ScaleIcon", Range(0, 1)) = 0.25
        _CircleA_Speed ("CircleA_Speed", Float ) = -0.5
        _CircleB_Speed ("CircleB_Speed", Float ) = 0.5
        _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.2
       
        [MaterialToggle] _UsePattern ("UsePattern", Float ) = 0
        _DashAmount ("DashAmount", Float ) = 64
        _DashSpeed ("DashSpeed", Float ) = 1
        _DashLength ("Dash_Length", Range(0, 1)) = 0.5

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

            float4 _Color;
            float _Level;
            float _FadeIn;
            float _Emission;
            float _Speed;
            fixed _UseSin;
            float _CenterRadius;
            float _Width;
            float4 _Color2;
            float _Radius;
            float _DashSpeed;
            sampler2D _Patterns; 
            float4 _Patterns_ST;
            float _ScaleIcon;
            float _CircleA_Speed;
            float _CircleB_Speed;
            float _BG_Opacity;
            float _DashAmount;
            fixed _UsePattern;
            float _DashLength;

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
           
            

            fixed4 frag (v2f i ) : SV_Target
            {
                float2 uv = i.uv;
                uv =TRANSFORM_TEX(uv, _Patterns);
                //画圆 画极坐标
                float2 ruv = uv*2-1;
                float circle = length(ruv);
                float polar = DrawPolar(ruv);
                //画内圆
                float innerCircle = saturate((circle - _CenterRadius) / (_Radius*0.85 - _CenterRadius));
                //分层
                float levels = 1+_Level;
                float layer = floor(innerCircle * levels) / (levels - 1);
                //根据分层渐变颜色
                float3 finalColor = lerp(_Color2.rgb,_Color.rgb,layer)*_Emission;
                
                //计算动效时间
                float sinSpeed = saturate(sin(_Time.y*_Speed)*0.5555556+0.5);
                float useSin = lerp( _FadeIn, sinSpeed, _UseSin );
                
                //中心圆 宽度 遮罩
                float centerCircle = levels*innerCircle;
                float circleWidth = step(_Width,1 - frac(centerCircle));
                float circleMask = (1 - floor(innerCircle));
                //画虚线
                float dashFlow =DrawDash(polar,circle,_DashAmount,_DashSpeed,_DashLength);
                
                

                
                //两个转动的圆环
                float circleA = AroundFlow(circle,polar,_CircleA_Speed,float2(0.95,0.92));
                float circleB = AroundFlow(circle,polar,_CircleB_Speed,float2(0.90,0.88));
                float circleFlow = step(0.2,circleA+circleB);
                //虚线+ 转动圆环
                float pattern = dashFlow+circleFlow;
                float usePattern = lerp( 0, pattern, _UsePattern );
                //缩放ICON
                float2 iconUV = ScaleUV(uv,_ScaleIcon);
                //采样ICON形状
                float iconShape = tex2D(_Patterns,iconUV).a;
                float fade = 1 - step(0.1,useSin);
                iconShape *=fade;
                

                centerCircle =step(0.1,centerCircle);
                
                float finalCircle =circleWidth*circleMask*centerCircle*_BG_Opacity;

                float circleSin = step((useSin+0.06),layer);
                
                float finalAphla = circleSin*circleWidth*circleMask+usePattern+iconShape+finalCircle;
                
                return fixed4(finalColor,finalAphla);
            
                
            }
            ENDCG
        }
    }
    
    
    
}
